import 'package:flutter/material.dart';
import 'package:flutter_ume/data/net/service_method.dart';
import 'package:flutter_vertical_marquee/flutter_vertical_marquee.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:flutter_ume/common/component_index.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;
  List<String> _tipMarqueeList = [];

  @override
  void initState() {
    initData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    var scrollController = ScrollController();
    /*获取数据*/

    // TODO: implement build
    final _startColor = Color(0xFFE1C087);
    final _endColor = Color(0xFFCAA364);

    return Scaffold(
      appBar: _appTitle(),
      body: ListView(
        scrollDirection: Axis.vertical,
        reverse: false,
        primary: false,
        physics: const AlwaysScrollableScrollPhysics(),
        controller: scrollController,
        children: <Widget>[
          /*顶部广告*/
          _anTop(),
          /*公告*/
          _notice(),
          Projce01(),
          Project0203(),
          NewUserRead()
          /**/
        ],
      ),
      backgroundColor: Colors.white,
    );
  }

  _initMarquee() {
    _tipMarqueeList.clear();
    _tipMarqueeList.addAll(["test1", "test2"]);
  }

  //标题
  Widget _appTitle() {
    return new AppBar(
      title: const Text(
        '联众金融',
        style: TextStyle(
            color: Colors.black, fontSize: 19, fontFamily: "PingFangBlod"),
      ),
      centerTitle: true,
      backgroundColor: Colors.white,
    );
  }

  //顶部广告
  Widget _anTop() {
    return Container(
      height: 71,
      margin: const EdgeInsets.fromLTRB(12, 8, 12, 0),
      decoration: new BoxDecoration(
        gradient: const LinearGradient(
            colors: [Color(0xFFE1C087), Color(0xFFCAA364)]), //渐变色
        borderRadius: new BorderRadius.circular(5.0),
        boxShadow: [
          //设置阴影
          BoxShadow(
              color: Color(0xFFCAA364), blurRadius: 5.0, spreadRadius: 3.0),
        ],
      ),
      child: new Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly, //让children 平分
        children: <Widget>[
          new Text(
            '精准额度',
            style: new TextStyle(
                color: Colors.white,
                fontSize: ScreenUtil.getInstance().setSp(46),
                fontFamily: "PingFangBlod"),
          ),
          new Text(
            '快速审批',
            style: new TextStyle(
                color: Colors.white,
                fontSize: ScreenUtil.getInstance().setSp(46),
                fontFamily: "PingFangBlod"),
          ),
          new Text(
            '息费透明',
            style: new TextStyle(
                color: Colors.white,
                fontSize: ScreenUtil.getInstance().setSp(46),
                fontFamily: "PingFangBlod"),
          ),
        ],
      ),
    );
  }

  //公告
  Widget _notice() {
    return Container(
        margin: EdgeInsets.fromLTRB(8, 19, 8, 0),
        child: new Row(
          children: <Widget>[
            new Image.asset(
              'resource/images/home_marquee.png',
              height: ScreenUtil().setHeight(38),
              width: ScreenUtil().setWidth(25),
            ),
            Container(
              width: ScreenUtil().setWidth(750 - 73),
              child: _buildMarquee(),
            )
          ],
        ));
  }

  /*公告*/
  Widget _buildMarquee() {
    //_initMarquee();
    var controller = MarqueeController();
    return GestureDetector(
      child: Container(
        height: 40.0,
        // color: Colors.white,
        child: Marquee(
          textList: _tipMarqueeList,
          // List<Text>, textList and textSpanList can only have one of code.
          fontSize: ScreenUtil.instance.setSp(30),
          // text size
          scrollDuration: Duration(seconds: 1),
          // every scroll duration
          stopDuration: Duration(seconds: 3),
          //every stop duration
          tapToNext: false,
          // tap to next
          textColor: Colors.black,
          // text color
          controller: controller, // the controller can get the position
        ),
      ),
      onTap: () {
        // get the position
        //跳转到详情
        print(controller.position); // get the position
      },
    );
  }

  //1.获取公告数据
  // 2.获取...
  void initData() {
    getHomeNoticeContent().then((val) {
      setState(() {
        print('获取公告数据：${val}');
        print('获取公告数据：${val['retObject']['rows']}');

        List list = val['retObject']['rows'];
        for (int i = 0; i < list.length; i++) {
          _tipMarqueeList.add(list[i]['title']);
        }
        /* var data = json.decode(val.toString());
        print('----------------------');
        Notice notice =  Notice.fromJson(data);
        print('标题:${notice.retObject.rows[0].title}');*/
      });
    });
  }
}

/*联众信用贷*/
class Projce01 extends StatelessWidget {
  //标题
  Widget _titleWidget() {
    return Container(
      height: ScreenUtil().setHeight(50),
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.fromLTRB(8.0, 0, 8.0, 0),
      /*  decoration: BoxDecoration(
        color: Colors.white,
       // borderRadius: new BorderRadius.all(new Radius.circular(20.0)),
      ),*/
      child: new Row(
        children: <Widget>[
          Container(
            width: ScreenUtil().setWidth(6),
            height: ScreenUtil().setHeight(32),
            color: Colors.amber,
          ),
          Container(
            child: Text(
              '联众信用贷',
              style: TextStyle(
                  fontSize: ScreenUtil().setSp(32), fontFamily: "PingFangBlod"),
            ),
            margin: const EdgeInsets.fromLTRB(8.0, 0, 0, 0),
          ),
        ],
      ),
    );
  }

  //项目展示
  Widget _tv0Widget() {
    return new Text(
      '企业最高可借',
      style: new TextStyle(
          fontFamily: "PingFangBlod", fontSize: ScreenUtil().setSp(38)),
    );
  }

  Widget _tv1Widget() {
    return new Text(
      '1,000,000.00',
      style: new TextStyle(
        fontFamily: "PingFangBlod",
        fontSize: ScreenUtil().setSp(60),
        color: Colours.baseTextColor,
      ),
    );
  }

  // 12个月 30分钟 0.05%
  //期限  到账  最低日利
  Widget _rowWidget() {
    return Container(
      margin: EdgeInsets.fromLTRB(0.0, 15.0, 0.0, 0.0),
      child: new Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
          new Column(children: [
            new Text(
              '12个月',
              style: new TextStyle(
                fontSize: ScreenUtil().setSp(24),
                fontFamily: AppFontFamily.PingFangMedium,
                color: Colours.tvBlack,
              ),
            ),
            new Text(
              '期限',
              style: new TextStyle(
                fontSize: ScreenUtil().setSp(24),
                fontFamily: AppFontFamily.PingFangMedium,
                color: Colours.tv9c,
              ),
            ),
          ]),
          new Column(children: [
            new Text(
              '30分钟',
              style: new TextStyle(
                fontSize: ScreenUtil().setSp(24),
                fontFamily: AppFontFamily.PingFangMedium,
                color: Colours.tvBlack,
              ),
            ),
            new Text(
              '到账',
              style: new TextStyle(
                fontSize: ScreenUtil().setSp(24),
                fontFamily: AppFontFamily.PingFangMedium,
                color: Colours.tv9c,
              ),
            ),
          ]),
          new Column(children: [
            new Text(
              '0.05%',
              style: new TextStyle(
                fontSize: ScreenUtil().setSp(24),
                fontFamily: AppFontFamily.PingFangMedium,
                color: Colours.tvBlack,
              ),
            ),
            new Text(
              '最低日历',
              style: new TextStyle(
                fontSize: ScreenUtil().setSp(24),
                fontFamily: AppFontFamily.PingFangMedium,
                color: Colours.tv9c,
              ),
            ),
          ]),
        ],
      ),
    );
  } // 12个月 30分钟 0.05%

  //按钮
  Widget _btnWidget() {
    return Container(
      margin: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 0.0),
      height: ScreenUtil().setHeight(70),
      width: ScreenUtil().setWidth(316),
      child: CupertinoButton(
        padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
        child: Text(
          '立即借款',
          style: TextStyle(fontSize: ScreenUtil().setSp(32)),
          textAlign: TextAlign.center,
        ),
        pressedOpacity: 0.8,
        // 按下后的按钮不透明度
        color: Colours.baseBtnColor,
        // 按钮颜色
        borderRadius: BorderRadius.all(Radius.circular(18.0)),
        onPressed: () {
          print('点击了【立即借款】');
        },
      ),
    );
  }

  //项目详情.
  Widget _detailsWidget() {
    return Container(
      margin: EdgeInsets.fromLTRB(0.0, 12, 0.0, 0.0),
      padding: EdgeInsets.fromLTRB(0.0, 22.0, 0.0, 0.0),
      child: new Column(
        children: <Widget>[
          _tv0Widget(),
          _tv1Widget(),
          _rowWidget(),
          _btnWidget()
        ],
      ),
      height: ScreenUtil().setHeight(379),
      width: ScreenUtil().setWidth(702),
      decoration: AppCardStyle.decoration,
    );
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      child: new Column(
        children: <Widget>[
          _titleWidget(),
          _detailsWidget(),
        ],
      ),
    );
  }

  Projce01({Key key}) : super(key: key);
}

//联众担保、抵押.
class Project0203 extends StatelessWidget {
  //name.
  Widget _nameWidget(String name) {
    return new Container(
      margin: EdgeInsets.fromLTRB(8.0, 0, 8.0, 0),
      child: new Row(
        children: <Widget>[
          Expanded(
            child: Text(
              name,
              style: TextStyle(
                  color: Colours.tvBlack,
                  fontSize: ScreenUtil().setSp(32),
                  fontFamily: AppFontFamily.PingFangBlod),
              textAlign: TextAlign.center,
            ),
            flex: 1,
          ),
          new Image.asset(
            'resource/images/icon_right.png',
            height: ScreenUtil().setHeight(50),
            width: ScreenUtil().setWidth(30),
          ),
        ],
      ),
    );
  }

  //rate.
  Widget _rateWidget(String rate) {
    return Text(rate,
        style: TextStyle(
            fontFamily: AppFontFamily.PingFangBlod,
            fontSize: ScreenUtil().setSp(46),
            color: Colours.baseTextColor),
        textAlign: TextAlign.center);
  }

  //msg.
  Widget _msgWidget(String rate) {
    return Text(rate,
        style: TextStyle(
            fontFamily: AppFontFamily.PingFangMedium,
            fontSize: ScreenUtil().setSp(24),
            color: Colours.tv9c),
        textAlign: TextAlign.center);
  }

  //联众抵押.
  Widget _leftWidget() {
    return new Container(
      decoration: AppCardStyle.decoration,
      height: ScreenUtil().setHeight(178),
      width: ScreenUtil().setWidth(340),
      child: new Column(
        children: <Widget>[
          _nameWidget('联众抵押贷款'),
          _rateWidget('0.08%'),
          _msgWidget('最低日利'),
        ],
      ),
    );
  }

  //联众担保.
  Widget _rightWidget() {
    return new Container(
      decoration: AppCardStyle.decoration,
      height: ScreenUtil().setHeight(178),
      width: ScreenUtil().setWidth(340),
      child: new Column(
        children: <Widget>[
          _nameWidget('联众担保贷款'),
          _rateWidget('8.0%'),
          _msgWidget('历史年化收益率'),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Container(
      margin: EdgeInsets.fromLTRB(8.0, 19.0, 8.0, 19.0),
      child: new Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[_leftWidget(), _rightWidget()],
      ),
    );
  }

  Project0203({Key key}) : super(key: key);
}

//新手必读
class NewUserRead extends StatelessWidget {
  //标题
  Widget _titleWidget() {
    return Container(
      height: ScreenUtil().setHeight(50),
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.fromLTRB(8.0, 0, 8.0, 0),
      /*  decoration: BoxDecoration(
        color: Colors.white,
       // borderRadius: new BorderRadius.all(new Radius.circular(20.0)),
      ),*/
      child: new Row(
        children: <Widget>[
          Container(
            width: ScreenUtil().setWidth(6),
            height: ScreenUtil().setHeight(32),
            color: Colors.amber,
          ),
          Container(
            child: Text(
              '新手必读',
              style: TextStyle(
                  fontSize: ScreenUtil().setSp(32), fontFamily: "PingFangBlod"),
            ),
            margin: const EdgeInsets.fromLTRB(8.0, 0, 0, 0),
          ),
        ],
      ),
    );
  }

  Widget _swiperBuilder(BuildContext context, int index) {
    List<Widget> imageList = List();
    imageList.add(Image.network(
      'http://jspang.com/static/upload/20181213/pvA-e9vkrNiXVFUm6xXMO9zf.jpg',
      fit: BoxFit.fill,
    ));
    imageList.add(Image.network(
      'https://jspang.com/static/upload/20190212/QFo4lsaVP38aHq2GhSekaHhY.jpg',
      fit: BoxFit.fill,
    ));
    imageList.add(Image.network(
      'http://jspang.com/static/upload/20190102/9SpnvSzav06JfPUrVtc1hNu9.jpg',
      fit: BoxFit.fill,
    ));
    return (imageList[index]);
  }

  //轮播图.
  Widget _swiperWidget() {
    return new Container(
        height: ScreenUtil().setHeight(194),
        child: new Swiper(
          itemBuilder: _swiperBuilder,
          itemCount: 3,
          pagination: new SwiperPagination(
            builder: DotSwiperPaginationBuilder(
                color: Colors.white70,
                size: 10,
                // 其他点的颜色
                activeColor:Colours.baseTextColor,
                // 当前点的颜色
                space: 2,
                // 点与点之间的距离
                activeSize: 10
                // 当前点的大小
                ),
          ),
          autoplay: true,
          duration: 300,
          index: 0,
        ));
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      child: new Column(
        children: <Widget>[_titleWidget(), _swiperWidget()],
      ),
    );
  }

  NewUserRead({Key key}) : super(key: key);
}
